import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from "@/views/LoginView.vue";
import Layout from "@/views/Layout";
import RegisterView from "@/views/RegisterView";
import PaperView from "@/views/PaperView";
import TypeView from "@/views/TypeView";
import LogView from "@/views/LogView";
import NoticeView from "@/views/NoticeView";
import AuditView from "@/views/AuditView";
import PasswordView from "@/views/PasswordView";
import AiView from "@/views/AiView";
import ExamView from "@/views/ExamView";
import ExamingView from "@/views/ExamingView";
import GradesView from "@/views/GradesView";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: Layout,
    children:[
      {
        path: '/home',
        name: 'home',
        component: HomeView
      },
      {
        path: '/admin',
        name: 'admin',
        component: () => import(/* webpackChunkName: "about" */ '../views/AdminView.vue')
      },
      {
        path: '/paper',
        name: 'paper',
        component: PaperView
      },
      {
        path: '/type',
        name: 'type',
        component: TypeView
      },
      {
        path: '/log',
        name: 'log',
        component: LogView
      },
      {
        path: '/notice',
        name: 'notice',
        component: NoticeView
      },
      {
        path: '/audit',
        name: 'audit',
        component: AuditView
      },
      {
        path: '/password',
        name: 'password',
        component: PasswordView
      },
      {
        path: '/ai',
        name: 'ai',
        component: AiView
      },
      {
        path: '/exam',
        name: 'exam',
        component: ExamView
      },
      {
        path: '/grades',
        name: 'grades',
        component: GradesView
      }
    ]
  },
  {
    path: '/examing',
    name: 'examing',
    component: ExamingView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
router.beforeEach((to,from,next)=>{
  const whiteList = ['/login', '/register']; // 添加 '/register'

  if (whiteList.includes(to.path)) {
    // 如果是白名单中的路由，直接放行
    next();
    return;
  }
  const user =localStorage.getItem("user");
  if(!user && to.path !=='/login'){
    return next("login");
  }
  next();
})

export default router
